Numpy gradients

Created by Mauro Alberti

Last run: 2019-06-16


In [1]:
import numpy as np

In [2]:
a= np.array([[1, 2, 3, 4], [4, 6, 8, 10], [9, 11, 13, 15]])

In [3]:
a


Out[3]:
array([[ 1,  2,  3,  4],
       [ 4,  6,  8, 10],
       [ 9, 11, 13, 15]])

In [4]:
np.gradient(a, axis=0)  # along y axis


Out[4]:
array([[3. , 4. , 5. , 6. ],
       [4. , 4.5, 5. , 5.5],
       [5. , 5. , 5. , 5. ]])

In [5]:
np.gradient(a, axis=1)  # along x axis


Out[5]:
array([[1., 1., 1., 1.],
       [2., 2., 2., 2.],
       [2., 2., 2., 2.]])

Test pygsf gradients and directional derivative


In [6]:
from math import pi
from numpy import array

In [7]:
from pygsf.spatial.rasters.fields import *

In [8]:
fld1 = array([
   [1, 1, 1, 1],
   [1, 1, 1, 1],
   [1, 1, 1, 1]])

In [9]:
grad_j(fld1, cell_size_j=10)


Out[9]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

In [10]:
grad_i(fld1, cell_size_i=10)


Out[10]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

In [11]:
fld2 = array([
   [10, 10, 10, 10],
   [20, 20, 20, 20],
   [30, 30, 30, 30]])

In [12]:
grad_j(fld2, cell_size_j=10)


Out[12]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

In [13]:
grad_i(fld2, cell_size_i=10)


Out[13]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

In [14]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*45.0/180.0)


Out[14]:
array([[-0.70710678, -0.70710678, -0.70710678, -0.70710678],
       [-0.70710678, -0.70710678, -0.70710678, -0.70710678],
       [-0.70710678, -0.70710678, -0.70710678, -0.70710678]])

In [15]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*0.0/180.0)


Out[15]:
array([[-1., -1., -1., -1.],
       [-1., -1., -1., -1.],
       [-1., -1., -1., -1.]])

In [16]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*180.0/180.0)


Out[16]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

In [17]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*90.0/180.0)


Out[17]:
array([[-6.123234e-17, -6.123234e-17, -6.123234e-17, -6.123234e-17],
       [-6.123234e-17, -6.123234e-17, -6.123234e-17, -6.123234e-17],
       [-6.123234e-17, -6.123234e-17, -6.123234e-17, -6.123234e-17]])

In [18]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*270.0/180.0)


Out[18]:
array([[1.8369702e-16, 1.8369702e-16, 1.8369702e-16, 1.8369702e-16],
       [1.8369702e-16, 1.8369702e-16, 1.8369702e-16, 1.8369702e-16],
       [1.8369702e-16, 1.8369702e-16, 1.8369702e-16, 1.8369702e-16]])

In [19]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*315.0/180.0)


Out[19]:
array([[-0.70710678, -0.70710678, -0.70710678, -0.70710678],
       [-0.70710678, -0.70710678, -0.70710678, -0.70710678],
       [-0.70710678, -0.70710678, -0.70710678, -0.70710678]])

In [20]:
dir_deriv(fld2, cell_size_x=10, cell_size_y=10, direct_rad=pi*135.0/180.0)


Out[20]:
array([[0.70710678, 0.70710678, 0.70710678, 0.70710678],
       [0.70710678, 0.70710678, 0.70710678, 0.70710678],
       [0.70710678, 0.70710678, 0.70710678, 0.70710678]])

Test pygsf orientations


In [21]:
fld3 = array([
   [10, 10, 10, 10],
   [20, 20, 20, 20],
   [30, 30, 30, 30]])

In [22]:
fld4 = fld3

In [23]:
orients_d(fld3, fld4)


Out[23]:
array([[45., 45., 45., 45.],
       [45., 45., 45., 45.],
       [45., 45., 45., 45.]])

In [24]:
orients_d(fld3, -fld4)


Out[24]:
array([[135., 135., 135., 135.],
       [135., 135., 135., 135.],
       [135., 135., 135., 135.]])

In [25]:
orients_d(-fld3, fld4)


Out[25]:
array([[315., 315., 315., 315.],
       [315., 315., 315., 315.],
       [315., 315., 315., 315.]])

In [26]:
orients_d(-fld3, -fld4)


Out[26]:
array([[225., 225., 225., 225.],
       [225., 225., 225., 225.],
       [225., 225., 225., 225.]])

In [27]:
orients_d(fld3, 0.0)


Out[27]:
array([[90., 90., 90., 90.],
       [90., 90., 90., 90.],
       [90., 90., 90., 90.]])

In [28]:
orients_d(0, fld4)


Out[28]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

In [29]:
orients_d(0, -fld4)


Out[29]:
array([[180., 180., 180., 180.],
       [180., 180., 180., 180.],
       [180., 180., 180., 180.]])

In [30]:
orients_d(-fld3, 0.0)


Out[30]:
array([[270., 270., 270., 270.],
       [270., 270., 270., 270.],
       [270., 270., 270., 270.]])